package ar.edu.unq.wannaplay.services;

import java.util.LinkedList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import ar.edu.unq.wannaplay.client.Client;
import ar.edu.unq.wannaplay.dtos.ClientDTO;
import ar.edu.unq.wannaplay.exceptions.BlankStringException;
import ar.edu.unq.wannaplay.exceptions.EmailSyntaxException;
import ar.edu.unq.wannaplay.persistence.daos.ClientDAO;
import ar.edu.unq.wannaplay.utils.EmailValidator;
import ar.edu.unq.wannaplay.utils.Service;

public class ClientService implements Service<Client, ClientDTO> {
    private static final long serialVersionUID = 1L;

    private EmailValidator emailValidator;

    private ClientDAO dao;

    @Override
    public ClientDTO toDto(final Client client) {
        return new ClientDTO(client);
    }

    @Transactional
    public void saveClient(final String name, final String email, final String address, final String town)
            throws BlankStringException, EmailSyntaxException {
        String trimName = name.trim();
        String trimEmail = email.trim();
        String trimAddress = address.trim();
        String trimTown = town.trim();

        if ("".equals(trimName) || "".equals(trimEmail) || "".equals(trimAddress) || "".equals(trimTown)) {
            throw new BlankStringException("The string is Empty");
        } else if (!emailValidator.validate(trimEmail)) {
            throw new EmailSyntaxException("Email not written properly");
        }

        Client client = new Client(trimName, trimEmail, trimAddress, trimTown);
        this.getDao().save(client);
    }

    @Transactional
    public void updateClient(final int id, final String name, final String email, final String address,
            final String town) throws BlankStringException, EmailSyntaxException {
        String trimName = name.trim();
        String trimEmail = email.trim();
        String trimAddress = address.trim();
        String trimTown = town.trim();

        if ("".equals(trimName) || "".equals(trimEmail) || "".equals(trimAddress) || "".equals(trimTown)) {
            throw new BlankStringException("The string is Empty");
        } else if (!emailValidator.validate(trimEmail)) {
            throw new EmailSyntaxException("Email not written properly");
        }

        Client client = this.getDao().findById(id);
        client.setName(trimName);
        client.setEmail(trimEmail);
        client.setAddress(trimAddress);
        client.setTown(trimTown);
        this.getDao().update(client);
    }

    @Transactional
    public List<ClientDTO> findAllDTOs() {
        return this.toDTOs(this.getDao().findAll());
    }

    private List<ClientDTO> toDTOs(final List<Client> clients) {
        List<ClientDTO> dtosClients = new LinkedList<ClientDTO>();
        for (Client client : clients) {
            dtosClients.add(this.toDto(client));
        }
        return dtosClients;
    }

    public ClientDAO getDao() {
        return dao;
    }

    public void setDao(final ClientDAO dao) {
        this.dao = dao;
    }

    public void setEmailValidator(final EmailValidator emailValidator) {
        this.emailValidator = emailValidator;
    }

    public EmailValidator getEmailValidator() {
        return emailValidator;
    }

    public Client findClientById(final int id) {
        return this.getDao().findById(id);
    }
}
